package smartgrid;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import org.hibernate.criterion.Restrictions;
import org.orm.PersistentException;
import smartgrid.exceptions.CantRemoveFromDBexception;
import smartgrid.exceptions.FacebookAccountDisabledException;
import smartgrid.exceptions.FacebookAccountNotExistException;

@Stateless
public class FacebookAccountManager implements FacebookAccountInterface {

    /**
     * @param UID Facebook Account ID
     * @return If account with UID exists -> true
     *         If Account has one or more contracts -> true
     *           Otherwise -> false
     */
    @Override
    public Boolean facebookAccountHasContracts(String uID) throws FacebookAccountDisabledException {
        try {
            smartgrid.FaceBookAccountCriteria faceCrit = new FaceBookAccountCriteria();
            faceCrit.add(Restrictions.eq("UId", uID));
            smartgrid.FaceBookAccount faceBookAccount = smartgrid.FaceBookAccount.loadFaceBookAccountByCriteria(faceCrit);
            if (faceBookAccount != null) {
                if (!faceBookAccount.getEnable()) {
                    throw new FacebookAccountDisabledException();
                }
                return (!faceBookAccount.contract_role.isEmpty());
            }

        } catch (PersistentException ex) {
            Logger.getLogger(FacebookAccountManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    /**
     * 
     * @param UID Facebook Account ID
     * @return returns one facebook account
     */
    @Override
    public FaceBookAccount getFacebookAccountByUID(String uID) throws FacebookAccountDisabledException {
        try {
            smartgrid.FaceBookAccountCriteria faceCrit = new FaceBookAccountCriteria();
            faceCrit.add(Restrictions.eq("UId", uID));
            smartgrid.FaceBookAccount faceBookAccount = smartgrid.FaceBookAccount.loadFaceBookAccountByCriteria(faceCrit);
            if (faceBookAccount != null) {
                if (!faceBookAccount.getEnable()) {
                    throw new FacebookAccountDisabledException();
                }
            }
            return faceBookAccount;
        } catch (PersistentException ex) {
            Logger.getLogger(FacebookAccountManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * 
     * @param UID Facebook Account ID
     * @param name Facebook Account user name
     * @param email Facebook Account email
     * @return If sucefull true
     */
    @Override
    public Boolean insertFacebookAccount(String uID, String name, String email) {
        try {
            smartgrid.FaceBookAccountCriteria faceCrit = new FaceBookAccountCriteria();
            faceCrit.add(Restrictions.eq("UId", uID));
            smartgrid.FaceBookAccount faceBookAccount = smartgrid.FaceBookAccount.loadFaceBookAccountByCriteria(faceCrit);
            if (faceBookAccount == null) {
                smartgrid.FaceBookAccount fbProcess = FaceBookAccount.createFaceBookAccount();
                fbProcess.setUId(uID.toString());
                fbProcess.setNome(name);
                fbProcess.setEMail(email);
                fbProcess.setEnable(true);
                return fbProcess.save();
            }
        } catch (PersistentException ex) {
            Logger.getLogger(FacebookAccountManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    /**
     * 
     * @param UID Facebook Account ID
     * @param contract Contract Number
     * @return If Succefull: true
     */
    @Override
    @Deprecated
    public Boolean addExistingContractToFacebookAccount(String uID, String contractNumber) {
        try {
            smartgrid.FaceBookAccountCriteria faceCrit = new FaceBookAccountCriteria();
            faceCrit.add(Restrictions.eq("UId", uID));
            smartgrid.FaceBookAccount faceBookAccount = smartgrid.FaceBookAccount.loadFaceBookAccountByCriteria(faceCrit);

            smartgrid.ContractCriteria crit = new ContractCriteria();
            crit.add(Restrictions.eq("Number", contractNumber));
            smartgrid.Contract contract = smartgrid.Contract.loadContractByCriteria(crit);

            faceBookAccount.contract_role.add(contract);
            faceBookAccount.save();
            return true;

        } catch (PersistentException ex) {
            Logger.getLogger(FacebookAccountManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    /**
     * Disable one Facebook Account
     * 
     * @param facebookUID Facebook Account UID
     * @throws FacebookAccountDisabledException
     * @throws FacebookAccountNotExistException
     * @throws CantRemoveFromDBexception 
     */
    @Override
    public void disableFacebookAccount(String facebookUID) throws FacebookAccountDisabledException, FacebookAccountNotExistException, CantRemoveFromDBexception {
        FaceBookAccountCriteria faceCrit;
        try {
            faceCrit = new FaceBookAccountCriteria();
            faceCrit.add(Restrictions.eq("UId", facebookUID));
            smartgrid.FaceBookAccount faceBookAccount = smartgrid.FaceBookAccount.loadFaceBookAccountByCriteria(faceCrit);
            if (faceBookAccount != null) {
                if (!faceBookAccount.getEnable()) {
                    throw new FacebookAccountDisabledException();
                } else {
                    faceBookAccount.setEnable(false);
                    if (!faceBookAccount.save()) {
                        throw new CantRemoveFromDBexception();
                    }
                }
            } else {
                throw new FacebookAccountNotExistException();
            }
        } catch (PersistentException ex) {
            Logger.getLogger(FacebookAccountManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void enableFacebookAccount(String facebookUID) throws FacebookAccountDisabledException, FacebookAccountNotExistException, CantRemoveFromDBexception {
        FaceBookAccountCriteria faceCrit;
        try {
            faceCrit = new FaceBookAccountCriteria();
            faceCrit.add(Restrictions.eq("UId", facebookUID));
            smartgrid.FaceBookAccount faceBookAccount = smartgrid.FaceBookAccount.loadFaceBookAccountByCriteria(faceCrit);
            if (faceBookAccount != null) {
                if (!faceBookAccount.getEnable()) {
                    throw new FacebookAccountDisabledException();
                } else {
                    faceBookAccount.setEnable(true);
                    if (!faceBookAccount.save()) {
                        throw new CantRemoveFromDBexception();
                    }
                }
            } else {
                throw new FacebookAccountNotExistException();
            }
        } catch (PersistentException ex) {
            Logger.getLogger(FacebookAccountManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /*       PARA TESTES             */
    public static void main(String[] args) {

        String uid = "317234";
        String contractNumber = "321fasf";
        String nome = "Andre Barbosfasdfa";
        String email = "adsfasdlbmail88@gmail.com";


        System.out.println("teste");
        FacebookAccountManager fb = new FacebookAccountManager();

        System.out.println("cenas " + fb.insertFacebookAccount(uid, nome, email));
        System.out.println("Contrato " + fb.addExistingContractToFacebookAccount(uid, contractNumber));
    }

    @Override
    public List<FaceBookAccount> getFacebookAccounts() {
        FaceBookAccount[] f = FaceBookAccount.listFaceBookAccountByQuery(null, null);
        return Arrays.asList(f);
    }
}
